package com.fifth.apartment.service.impl;

import com.fifth.apartment.result.Result;
import com.fifth.apartment.service.GeoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/14
 */
@Service
@Slf4j
public class GeoServiceImpl implements GeoService {

    public  static final  String SIT_KEY = "site";
    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    @Override
    public Result addGeo() {
        Map<Object, Point> map = new HashMap<>();
        map.put("巨蟹座",new Point(120.37756,31.494236));
        map.put("鼋头渚",new Point(120.270792,31.508785));
        map.put("锡惠公园",new Point(120.270792,31.508785));
        map.put("桃花源",new Point(120.270792,31.508785));
        Long count = redisTemplate.opsForGeo().add(SIT_KEY,map);
        log.info("count {}",count);
        return new Result(true,"添加成功");
    }

    @Override
    public GeoResults addPoint(String longitude, String latitude) {
        double longitudes = Double.parseDouble(longitude);
        double latitudes = Double.parseDouble(latitude);
        Circle circle = new Circle(longitudes,latitudes,5000);
        RedisGeoCommands.GeoRadiusCommandArgs args =
                RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeCoordinates().includeDistance().sortAscending().limit(3);
        GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = redisTemplate.opsForGeo().radius(SIT_KEY,circle,args);
        return geoResults;
    }

    @Override
    public GeoResults findBySite(String site) {
        Distance distance = new Distance(20,RedisGeoCommands.DistanceUnit.KILOMETERS);
        RedisGeoCommands.GeoRadiusCommandArgs args =
                RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending().limit(2);
        GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = redisTemplate.opsForGeo().radius(SIT_KEY, site, distance, args);
        return geoResults;
    }
}
